package com.runa.protocol.dto;

import com.runa.protocol.enums.COTPType;

public class COTPSectionData {

    private COTPType cotpType;

    private int dstRef = 0;

    private int srcRef = 1;

    private byte option = 0;

    private int tpduSize = 480;

    private int srcTsap = 0x0102;

    private int flag = 1;

    private int rack = 0;

    private int slot = 0;

    private boolean isLast = true;

    private int tpdNum = 0;

    public COTPSectionData(COTPType cotpType) {
        this.cotpType = cotpType;
    }

    public COTPType getCotpType() {
        return cotpType;
    }

    public int getDstRef() {
        return dstRef;
    }

    public void setDstRef(int dstRef) {
        this.dstRef = dstRef;
    }

    public int getSrcRef() {
        return srcRef;
    }

    public void setSrcRef(int srcRef) {
        this.srcRef = srcRef;
    }

    public byte getOption() {
        return option;
    }

    public void setOption(byte option) {
        this.option = option;
    }

    public int getTpduSize() {
        return tpduSize;
    }

    public void setTpduSize(int tpduSize) {
        this.tpduSize = tpduSize;
    }

    public int getSrcTsap() {
        return srcTsap;
    }

    public void setSrcTsap(int srcTsap) {
        this.srcTsap = srcTsap;
    }

    public int getFlag() {
        return flag;
    }

    public void setFlag(int flag) {
        this.flag = flag;
    }

    public int getRack() {
        return rack;
    }

    public void setRack(int rack) {
        this.rack = rack;
    }

    public int getSlot() {
        return slot;
    }

    public void setSlot(int slot) {
        this.slot = slot;
    }

    public void setCotpType(COTPType cotpType) {
        this.cotpType = cotpType;
    }

    public boolean isLast() {
        return isLast;
    }

    public void setLast(boolean last) {
        isLast = last;
    }

    public int getTpdNum() {
        return tpdNum;
    }

    public void setTpdNum(int tpdNum) {
        this.tpdNum = tpdNum;
    }

    public byte[] toBytes() {
        switch (cotpType) {
            case ConnectReq:
                byte[] bytes = new byte[18];
                bytes[0] = 17;
                bytes[1] = cotpType.getCode();
                bytes[2] = (byte) (dstRef >> 8);
                bytes[3] = (byte) dstRef;
                bytes[4] = (byte) (srcRef >> 8);
                bytes[5] = (byte) srcRef;
                bytes[6] = option;
                bytes[7] = (byte) 0xc0;
                bytes[8] = 1;
                bytes[9] = 10;
                bytes[10] = (byte) 0xc1;
                bytes[11] = 2;
                bytes[12] = (byte) (srcTsap >> 8);
                bytes[13] = (byte) srcTsap;
                bytes[14] = (byte) 0xc2;
                bytes[15] = 2;
                bytes[16] = (byte) flag;
                bytes[17] = (byte) (rack << 4 & slot);
                return bytes;
            case ConnectConfirm:
                bytes = new byte[3];
                bytes[0] = 2;
                bytes[1] = cotpType.getCode();
                bytes[2] = (byte) (isLast ? tpdNum & 0x7f : tpdNum & 0x7f | 0x80);
                return bytes;
        }
        return null;
    }

}
